package com.hna.eking.ZkUtils;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hna.eking.Redis.*;

public class MainTest {
	private static final Logger logger = LoggerFactory.getLogger(MainTest.class);

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// RedisTest();
		TestLock();
//		SyncTest();
	}
	public static void TestLock(){
		
		logger.debug("Condition await!");
		ZkWatcher zk = new ZkWatcher();
		zk.setHosts("10.71.200.109:2181");
		zk.setSessionID("testsession");
		zk.setUUID("111111");
		zk.InitWatcher();
		logger.debug("waiting");
		zk.WaitLock();
		logger.debug("wait complete");
		zk.CloseWatcher();
	}
	public static void WatcherTest() {
		logger.debug("Begin Run Test Main");
		ZooKeeper zk = null;
		String FilePath = "/TestPath";
		int version;
		try {
			ZkWatcher zkwatch = new ZkWatcher();
			zk = new ZooKeeper("10.71.200.109:2181", 10000, zkwatch);
			logger.debug("zk connected");
			Stat st = zk.exists(FilePath, false);

			if (st == null) {
				logger.debug("file " + FilePath + " not exists");
				zk.create(FilePath, "1111111".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
				logger.debug("FilePath created");
			}
			
		//	version = st.getVersion();
		//	logger.debug("getVersion data is: " + version);
			zk.setData(FilePath, "222222".getBytes(), -1);
			logger.debug("set data success");
			Object mutex = new Object();
			zkwatch.mutex = mutex;
			String ret = new String(zk.getData(FilePath, zkwatch, st));
			logger.debug("file context: " + ret);
			
			mutex.wait();
			logger.debug("watched");
			version = st.getVersion();
			zk.delete(FilePath, -1);
			logger.debug("file delete");
		} catch (IOException | KeeperException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				logger.debug("finally run here ");
				zk.close();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void SyncTest(){
		logger.debug("Begin Run Test Main");
		ZooKeeper zk = null;
		try {
			zk = new ZooKeeper("10.71.200.109:2181", 10000, new ZkWatcher());
			logger.debug("zk connected");
			String FilePath = "/TestPath";
			Stat st = new Stat();
			String ret = new String(zk.getData(FilePath, new ZkWatcher(), st));
			logger.debug("file context: " + ret);
		} catch (IOException | KeeperException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				zk.close();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	public static void RedisTest() {
		logger.debug("Begin Run Test Main");
		RedisUtils redis = new RedisUtils("10.71.200.109", 6379);

		redis.InitJRedis();

		if (redis.IsConnect()) {
			logger.info("Connect Redis Success");
		} else {
			logger.error("Connect Redis failed");
			return;
		}

		// logger.info(redis.RedisPing());
		redis.SetStrContext("0001", "input 0001 string success");
		logger.info("input key success");

		String Ret = redis.GetStrContext("0001");
		logger.info("get value:" + Ret);

		redis.DelStrKey("0001");
		logger.info("delete Redis Success");
		redis.CloseRedis();
		logger.info("close Redis Success");
	}

}
